package ru.spbu.mm.diploma.client.controller;

import org.apache.log4j.Logger;
import ru.spbu.mm.diploma.client.service.IUserroleConstants;
import ru.spbu.mm.diploma.client.service.RightsService;
import ru.spbu.mm.diploma.client.service.SessionService;
import ru.spbu.mm.diploma.common.entity.User;
import ru.spbu.mm.diploma.common.facade.UserFacadeImpl;
import ru.spbu.mm.diploma.common.resources.MessageFactory;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 *
 */
public class LoginController implements IUserroleConstants{
    private static final Logger LOG = Logger.getLogger(LoginController.class.getName());

    public static final String SESSION_ATTR_USERROLE = "USERROLE_ID";
    public static final String SESSION_ATTR_USER = "USER_ID";

    private User user;

    public LoginController() {
        user = new User();
        LOG.info("Login Controller instance created.");
    }

    public String checkUser() {
        FacesContext context = FacesContext.getCurrentInstance();
        MessageFactory mf = new MessageFactory();
        if (doLogin()) {
            RightsService rightsService = new RightsService();
            rightsService.initRightsMap();
            LOG.info("Login sucessful");
            HttpSession session = SessionService.getInstance().getSession();
            if (((Integer) session.getAttribute(SESSION_ATTR_USERROLE)).equals(USERROLE_ADMIN_ID)) {
                return "success_admin";
            }
            return "success";
        } else {
            context.addMessage("loginForm:login",
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, mf.getMessage("errorUserData"), null));
            LOG.info("Incorrect user data");
            user.setUsername("");
            user.setPassword("");
            return "failure";
        }
    }

    public String logOut() throws IOException {
        HttpSession session = SessionService.getInstance().getSession();
        session.removeAttribute(LoginController.SESSION_ATTR_USER);
        session.removeAttribute(LoginController.SESSION_ATTR_USERROLE);
        SessionService.getInstance().getResponse().sendRedirect(SessionService.getInstance().getSession().getServletContext().getContextPath());
        session.invalidate();
        return "logout";
    }

    private boolean doLogin() {
        boolean status = false;
        User curr_user = UserFacadeImpl.getInstance().getUserWithUsername(user.getUsername());
        if (curr_user != null) {
            status = curr_user.getPassword().equals(user.getPassword());
            if (status) {
                HttpSession session = SessionService.getInstance().getSession();
                session.setAttribute(LoginController.SESSION_ATTR_USER, curr_user.getId());
                session.setAttribute(LoginController.SESSION_ATTR_USERROLE, curr_user.getUserroleID());
            }
        }
        return status;

    }

    public User getUser() {
        return this.user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}
